package com.chen.gui.component.folder;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.JComponent;

/**
 * @author 5
 * 
 */
public class SlidePane extends JComponent {

	private static final long serialVersionUID = 3774951882035772079L;

	private JComponent top;

	private boolean expand;

	private float ratio = 1;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

	public SlidePane() {
		setLayout(new SlideLayout());
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (top != null && !expand) {

		}
	}

	@Override
	protected void printChildren(Graphics g) {
		super.printChildren(g);
		if (top != null && expand) {

		}
	}

	public void setTopComponent(JComponent top) {
		if (this.top == top)
			return;

		if (top != null)
			remove(top);

		if (top != null)
			add(top);
	}

	class SlideLayout implements LayoutManager {

		@Override
		public void addLayoutComponent(String name, Component comp) {
		}

		@Override
		public void layoutContainer(Container target) {
			synchronized (target.getTreeLock()) {
				Insets insets = target.getInsets();
				int width = target.getWidth() - (insets.left + insets.right);
				int x = insets.left;
				int y = insets.top;

				if (top != null) {
					int h = (int) ((top.getPreferredSize().getHeight() - y - insets.bottom) * ratio);
					top.setBounds(x, y, width, h);
				}
			}
		}

		@Override
		public Dimension minimumLayoutSize(Container parent) {
			return null;
		}

		@Override
		public Dimension preferredLayoutSize(Container parent) {
			Insets insets = parent.getInsets();
			int w = insets.left + insets.right;
			int h = insets.top + insets.bottom;
			Dimension preferredSize = top.getPreferredSize();
			w += preferredSize.width;
			h += preferredSize.height * ratio;
			return new Dimension(w, h);
		}

		@Override
		public void removeLayoutComponent(Component comp) {
		}

	}
}
